package com.daJiao.domain;

import com.daJiao.entity.SysUser;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class SecurityUser implements UserDetails {

	private static final long serialVersionUID = 5800599093391505463L;

	/**
	 * 是否记住密码
	 */
	private Boolean remember=false;

	private Long userId;

	/**
	 * 用户名
	 */
	private String username;

	private String realName;

	private String email;

	private String sex;

	private String mobile;
	/**
	 * 用户密码
	 */
	private String password;

	private List<String> ownRoles;

	private boolean admin;


	public SecurityUser(SysUser user, List<String> roles) {
		if (user != null) {
			this.setUserId(user.getId());
			this.setUsername(user.getUserName());
			this.setPassword(user.getPassword());
			this.setRealName((user.getRealName()));
			this.setMobile(user.getMobile());
			this.setEmail(user.getEmail());
			this.setSex(user.getSex());
			this.ownRoles = roles;
		}
	}


	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		List<GrantedAuthority> grantedAuthorities =
				ownRoles.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
		return grantedAuthorities;
	}

	@Override
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}


	@Override
	public boolean isAccountNonExpired() {
		return true;
	}


	@Override
	public boolean isAccountNonLocked() {
		return true;
	}


	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}


	@Override
	public boolean isEnabled() {
		return true;
	}

	public Boolean getRemember() {
		return remember;
	}

	public void setRemember(Boolean remember) {
		this.remember = remember;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}


	public boolean isAdmin() {
		return (ownRoles.contains("admin"));
	}

	public void setAdmin(boolean admin) {
		this.admin = admin;
	}

}
